#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: api19File.py 
@version:
@time: 2022/06/21 
"""
from TapClientCore.T02_tm_api.v2_api.v2_api_base import *
from requests_toolbelt import MultipartEncoder
import string, json, random


class Api19FileUrl(V2ApiUrlBase):
    url_file = api_joint(
        V2ApiUrlBase().server, 'file'
    )
    url_file_upload = api_joint(
        url_file, 'upload'
    )


class Api19File(V2ApiBase, Api19FileUrl):

    def post_file_upload(self, files):
        filename = files.split('\\')[-1]
        file = open(files, 'rb')
        body = MultipartEncoder(
            fields={
                'id': 'robot',
                'name': filename,
                'type': 'application/octet-stream',
                'file': (filename, file, "application/octet-stream")
            },
            boundary=''.join(random.sample(string.ascii_letters + string.digits, 30))
        )
        headers = {'content-type': body.content_type}
        self.session.headers.update(headers)
        response = self.tapclient(
            self.post,
            self.url_file_upload
            + self.access_token,
            data=body,
        )
        file.close()
        self.session.headers.update({'content-type': 'application/json'})
        return response


if __name__ == '__main__':
    T = Api19File()
    from TapClientCore.T01_init_conf.path_conf import PathConf

    res = T.post_file_upload(PathConf.JAR_DEMO_PATH)
    print(res)
